home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / graphics / sfwjpg / doc / details.txt next >
Text File  |  1999-06-15  |  9KB  |  227 lines

  1. #
  2. # details.txt
  3. # Copyright (c) 1997-1999  Everett Lipman
  4. #
  5. # This document is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. # This document is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. # The GNU General Public License is available at
  14. #
  15. #    http://www.fsf.org/copyleft/gpl.html
  16. #
  17. # Alternatively, you can write to the 
  18. #
  19. #    Free Software Foundation, Inc.
  20. #    59 Temple Place - Suite 330
  21. #    Boston, MA  02111-1307, USA
  22. #
  23.  
  24. #
  25. # 9 May 1999
  26. #
  27.  
  28. This file contains instructions for the compilation of, and
  29. information about the function of, the programs sfwjpg, pwpjpg, and
  30. flipjpeg.
  31.  
  32. NOTE: The Independent JPEG group's jpegtran program now performs
  33.       the function of flipjpeg, which was written before the present
  34.       version of jpegtran became available.
  35.  
  36.       Seattle FilmWorks recently began scanning pictures at
  37.       resolutions other than 640x480.  This change affects all
  38.       resolutions referred to throughout this document.
  39.  
  40.  
  41. Compilation:
  42.  
  43.   sfwjpg and pwpjpg
  44.  
  45.     sfwjpg.c and pwpjpg.c are standalone ANSI C programs which should
  46.     compile without any modifications under UNIX.  Compilation under
  47.     Windows with Microsoft Visual C++ requires that the strcasecmp()
  48.     function, which occurs in both programs, be replaced with
  49.     strcmp().
  50.  
  51.   flipjpeg - See NOTE above
  52.  
  53.     flipjpeg.c is an ANSI C program which uses the Independent JPEG
  54.     Group's (IJG) free JPEG software, which you can find at
  55.     ftp://ftp.uu.net/graphics/jpeg.  flipjpeg.c uses standard input
  56.     and output, and has only been tested under UNIX.  It is probably
  57.     possible to compile flipjpeg.c on another platform with a few
  58.     modifications, such as changing the input and output files.  See
  59.     the IJG software documentation for more information.
  60.  
  61.     In order to compile flipjpeg.c under UNIX, unpack the IJG
  62.     software and place flipjpeg.c in the same directory.  After
  63.     running 'configure' to create the Makefile for the IJG software,
  64.     add the following lines to Makefile:
  65.  
  66.     FJOBJECTS= flipjpeg.o rdswitch.o cdjpeg.o
  67.  
  68.     flipjpeg: $(FJOBJECTS) libjpeg.a
  69.         $(LN) $(LDFLAGS) -o flipjpeg $(FJOBJECTS) libjpeg.a $(LDLIBS)
  70.  
  71.     then type
  72.  
  73.       make flipjpeg
  74.  
  75.  
  76.  
  77. Program Operation
  78.  
  79.   This section contains a technical description of the function of
  80.   sfwjpg, pwpjpg, and flipjpeg.  If you need more information about
  81.   JPEG compression or the JFIF file format, you can consult
  82.  
  83.   THE JPEG STILL IMAGE DATA COMPRESSION STANDARD
  84.   by William Pennebaker and Joan Mitchell
  85.   Van Nostrand Reinhold, 1993
  86.  
  87.   The information presented here about .sfw files has been deduced
  88.   through examination of a number of example files, and may not be
  89.   complete.  None of this information has been supplied or confirmed
  90.   by Seattle FilmWorks.
  91.  
  92.   sfwjpg
  93.  
  94.     sfwjpg converts a file in the .sfw format to the standard JPEG
  95.     File Interchange Format (JFIF).  This is not terribly difficult,
  96.     since a .sfw file is really a JFIF file with a few
  97.     modifications.  Here are the steps in the conversion, as
  98.     performed by sfwjpg:
  99.  
  100.     1.  Find the start of the JFIF data.
  101.         In a .sfw file, this is delineated by the four byte
  102.         sequence
  103.  
  104.         0xff 0xc8 0xff 0xd0
  105.  
  106.         this sequence must be changed to 
  107.  
  108.         0xff 0xd8 0xff 0xe0
  109.  
  110.         These four bytes become the start of the JFIF (.jpg) file,
  111.         all prior data having been discarded.
  112.  
  113.     2.  Place JFIF identifier in the proper place.
  114.         The following 7 byte string must appear after the first six
  115.         bytes, which are the 4 initial bytes from step 1, plus two
  116.         others, which indicate the length of the data preceding
  117.         the next marker:
  118.  
  119.         "JFIF\0\001\0"  Note: no implicit trailing null in this string.
  120.  
  121.     3.  Fix the other JPEG markers in the file.
  122.         Two byte sequences beginning with 0xff, such as those in
  123.         step 1, are called markers, and indicate what type
  124.         of data follows in the JPEG stream or JFIF file.  In the
  125.         .sfw file, the following markers must be converted back
  126.         to those specified in the JPEG standard.  The .sfw
  127.         markers are on the left, and the corresponding JPEG
  128.         markers are on the right:
  129.  
  130.         0xff 0xa0  ->  0xff 0xc0
  131.         0xff 0xa4  ->  0xff 0xc4
  132.         0xff 0xc8  ->  0xff 0xd8   (done in step 1)
  133.         0xff 0xc9  ->  0xff 0xd9
  134.         0xff 0xca  ->  0xff 0xda
  135.         0xff 0xcb  ->  0xff 0xdb
  136.         0xff 0xd0  ->  0xff 0xe0   (done in step 1)
  137.  
  138.     4.  Remove trailing data after the 0xff 0xd9 (0xc9 before correction)
  139.         marker near the end of the file.
  140.     
  141.     5.  Insert the Huffman coding table if it is not present in the
  142.         .sfw file.  This table, along with its marker, can be found in
  143.         sfwjpg.c.  The table should be inserted just before the
  144.         0xff 0xda (0xca before correction) marker.
  145.  
  146.     Once these steps have been carried out, you will have a JFIF file
  147.     containing your image.  The image will still need to be flipped
  148.     vertically.
  149.  
  150.   pwpjpg
  151.   
  152.     A .pwp file consists of a group of .sfw files (typically all of
  153.     the pictures from a roll of film) which have had some additional
  154.     information prepended to them (such as the file name and length).
  155.     pwpjpg reads the length and file name information, and then passes
  156.     each .sfw file to the same routine used in sfwjpg, which converts
  157.     the file to JFIF and writes it to disk.
  158.  
  159.     The beginning of the first filename is located at byte 0x14 in the
  160.     .pwp file, with byte 0x00 being the first byte in the file.  The
  161.     start of the .sfw file corresponding to that filename can be
  162.     found by searching for "SFW94A".  The length of the .sfw file is
  163.     stored in little-endian order.  If the char pointer pwpptr
  164.     points to the start of the "SFW94A" string, the three bytes
  165.     of the file length can be found at pwpptr-0x0A, pwpptr-0x0B,
  166.     and pwpptr-0x0C.  The byte at pwpptr-0x0A is the most
  167.     significant.  The next filename begins right after the end
  168.     of the previous .sfw file.  See the pwpjpg.c code for
  169.     further details.
  170.  
  171.   flipjpeg
  172.  
  173.     flipjpeg does a lossless inversion of the image in a JFIF file.
  174.  
  175.     The JPEG compression process consists of a number of steps.
  176.     My (probably incomplete and possibly incorrect) understanding 
  177.     of this process is as follows:
  178.  
  179.     1.  The image to be compressed is converted to a
  180.         representation using the desired components, for
  181.         example, Y, Cb and Cr instead of R, G and B.
  182.  
  183.     2.  Each component is broken up into 8x8 pixel blocks, and
  184.         a two dimensional discrete cosine transform (DCT) is
  185.         performed on each block of pixels.  Sometimes at this
  186.         stage, various components are reduced in resolution.  In
  187.         .sfw files, the Y component is kept at 640x480, and the
  188.         Cb and Cr components are reduced to 320x240.
  189.  
  190.     4.  The DCT coefficients are "quantized" (reduced in numerical,
  191.         not spatial, resolution) in order to reduce the amount of
  192.         information in the image in such a way that the appearance
  193.         of the image will not be noticeably affected.
  194.  
  195.     5.  The zero frequency coefficients for each block are written
  196.         so that they represent the change in the coefficient from the
  197.         previous block instead of the actual value of the coefficient.
  198.  
  199.     6.  The coefficients are compressed using Huffman coding, and the
  200.         resulting data are written out along with some markers to
  201.         delineate coding tables, etc.
  202.  
  203.     In order to do a lossless inversion, flipjpeg operates on raw DCT
  204.     coefficients.  First, it uses the Independent JPEG Group's
  205.     (excellent) software to read the DCT coefficients for each
  206.     component into an array.  It then goes through each array,
  207.     flipping the sign of every DCT coefficient which corresponds to a
  208.     basis function that is asymmetric in the vertical direction.
  209.     This operation has the effect of vertically flipping each of the
  210.     8 pixel high horizontal stripes in the image.  The coefficient
  211.     block rows are finally rearranged in the proper order, and the
  212.     file is written out, again using the IJG software.
  213.  
  214.     This procedure works fine on .sfw-derived JPEG files, since
  215.     Seattle FilmWorks scans images at 640x480 resolution, and 480
  216.     happens to be a multiple of 16.  When the image height is not a
  217.     multiple of 8, the remaining pixel rows in the last block are
  218.     filled with the same values as the last row in the image.  If the
  219.     components of the image are not sampled at the same resolution
  220.     (and perhaps sometimes even if they are) the different components
  221.     will be offset from each other in the flipped image.  This can be
  222.     seen if flipjpeg is run on testimg.jpg from the IJG software
  223.     distribution.
  224.